import { queryChannelInfo, queryRecommend, queryRecommendRecord, removeRecommendRecord, addRecommendRecord } from '../services/api';

export default {
  namespace: 'recommendRecord',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    dynmicColumns: [],
    dynmicChannels: [],
  },

  effects: {
    *init({ payload }, { call, put }) { // 加载动态列 和来源渠道
      const response = yield call(queryRecommend, payload);
      yield put({
        type: 'dynmicColumns',
        payload: response.list,
      });
      const response1 = yield call(queryChannelInfo, payload);
      yield put({
        type: 'dynmicChannels',
        payload: response1,
      });
    },
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryRecommendRecord, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addRecommendRecord, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeRecommendRecord, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    dynmicColumns(state, action) {
      return {
        ...state,
        dynmicColumns: action.payload,
      };
    },
    dynmicChannels(state, action) {
      return {
        ...state,
        dynmicChannels: action.payload,
      };
    },
  },
};
